<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="css/index.css"/>
    <script src="js/vue.js"></script>
    <script src="js/axios.min.js"> </script>
    <script src="js/index.js"></script>
    <script src="js/qs.min.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-header,
        .el-footer {
            background-color: #B3C0D1;
            color: #333;
            text-align: center;
            line-height: 60px;
        }

        .el-aside {
            background-color: #D3DCE6;
            color: #333;
            text-align: center;
            line-height: 20px;
        }

        .el-main {
            background-color: #E9EEF3;
            color: #333;
            text-align: center;

        }
        .el-menu {
            height: 700px;
        }
        body > .el-container {
            margin-bottom: 40px;
        }
        .el-container:nth-child(5) .el-aside,
        .el-container:nth-child(6) .el-aside {
            line-height: 260px;
        }

        .el-container:nth-child(7) .el-aside {
            line-height: 320px;
        }

    </style>
</head>
<body>
<div id="app">
    <el-container>
        <el-header>Header</el-header>
        <el-container>


            <el-aside width="150px">
                <el-button type="primary" icon="el-icon-plus" @click="addEmp">添加
                </el-button>
            </el-aside>

            <el-dialog title="提示"
                       :visible.sync="dialogVisible1"
                       width="30%">
                <el-form  label-width="80px" :model="addForm">
                    <el-upload
                            class="avatar-uploader"
                            action="/upload1"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                    <el-form-item label="员工姓名" prop="name">
                        <el-input v-model="addForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="员工年龄" prop="age">
                        <el-input v-model="addForm.age"></el-input>
                    </el-form-item>
                    <el-form-item label="岗位" prop="job">
                        <el-input v-model="addForm.job"></el-input>
                    </el-form-item>
                    <el-form-item label="入职日期" prop="entrydate">
                        <el-date-picker
                                v-model="addForm.entrydate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                style="width: 100%"
                                placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="薪水" prop="salary">
                        <el-input v-model="addForm.salary"></el-input>
                    </el-form-item>
                    <el-form-item label="所在部门" prop="deptId">
                        <el-select v-model="addForm.deptId" placeholder="请选择所在部门" style="width: 100%">
                            <el-option
                                    v-for="item in depts"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                        <el-button @click="dialogVisible1 = false">取 消</el-button>
                        <el-button type="primary" @click="add">确认添加</el-button>
                      </span>
            </el-dialog>

            <el-container>
                <el-main>Main

                    <div class="block">
                    <el-form :inline="true" :model="empSearchForm" class="demo-form-inline">
                        <el-form-item label="姓名">
                            <el-input v-model="empSearchForm.name" placeholder="姓名"></el-input>
                        </el-form-item>

                            <span class="demonstration">默认</span>
                            <el-date-picker
                                    v-model="empSearchForm.date"
                                    type="datetimerange"
                                    value-format="yyyy-MM-dd"
                                    range-separator="至"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期">
                            </el-date-picker>
                        <el-form-item>
                            <el-button type="primary" @click="onSubmit">查询</el-button>
                        </el-form-item>
                    </el-form>
                        </div>



                    <el-table :data="tableData" border style="width: 100%">
                        <el-table-column prop="id" label="编号">
                        </el-table-column>
                        <el-table-column prop="headimg" label="员工头像">
                            <template slot-scope="scope">
                                <img :src="scope.row.headimg" width="50" height="50"/>
                            </template>
                        </el-table-column>
                        <el-table-column prop="id" label="序号">
                        </el-table-column>
                        <el-table-column prop="name" label="姓名">
                        </el-table-column>
                        <el-table-column prop="age" label="年龄">
                        </el-table-column>
                        <el-table-column prop="job" label="工作">
                        </el-table-column>
                        <el-table-column prop="salary" label="薪水">
                        </el-table-column>
                        <el-table-column prop="entrydate" label="日期">
                        </el-table-column>
                        <el-table-column fixed="right" label="操作" width="200">
                            <template slot-scope="scope">
                                <el-button type="success" @click="editEmp(scope.row)">修改</el-button>
                                <el-button type="danger" @click="del(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="currentPage"
                            :page-sizes="[5,10,15,20]"
                            :page-size="pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="total">
                    </el-pagination>
                    <el-dialog title="提示"
                               :visible.sync="dialogVisible"
                               width="30%">
                        <el-form  label-width="80px" :model="editForm">
                            <el-form-item label="员工姓名">
                                <el-input v-model="editForm.name"></el-input>
                            </el-form-item>
                            <el-form-item label="员工年龄">
                                <el-input v-model="editForm.age"></el-input>
                            </el-form-item>
                            <el-form-item label="岗位">
                                <el-input v-model="editForm.job"></el-input>
                            </el-form-item>
                            <el-form-item label="入职日期">
                                <el-date-picker
                                        v-model="editForm.entrydate"
                                        type="date"
                                        value-format="yyyy-MM-dd"
                                        style="width: 100%"
                                        placeholder="选择日期">
                                </el-date-picker>
                            </el-form-item>
                            <el-form-item label="薪水">
                                <el-input v-model="editForm.salary"></el-input>
                            </el-form-item>
                            <el-form-item label="所在部门">
                                <el-select v-model="editForm.deptId" placeholder="请选择所在部门" style="width: 100%">
                                    <el-option
                                            v-for="item in depts"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-form>
                        <span slot="footer" class="dialog-footer">
                        <el-button @click="dialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="confirmUpdate">确认修改</el-button>
                      </span>
                    </el-dialog>
                </el-main>
                <el-footer>Footer</el-footer>
            </el-container>
        </el-container>
    </el-container>
</div>



<script>
    var app = new Vue({
        el: "#app",
        data: {
            tableData: [],
            dialogVisible:false,
            dialogVisible1:false,
            editForm:{},
            addForm:{},
            depts:[],
            imageUrl:"",
            currentPage:1,
            total:0,
            pageSize:5,
            empSearchForm: {},

        },
        created(){
            this.initTable();
        },
        methods:{


            handleSizeChange(val) {
                this.pageSize=val;
                this.initTable();
            },
            handleCurrentChange(val) {
               this.currentPage=val;
               this.initTable();
            },


            handleAvatarSuccess(res, file) {
                this.imageUrl = res.data;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 10;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            dialogClose(){
                this.imageUrl="";
                this.$refs.addFormRef.resetFields();
            },
            addEmp(){

                this.dialogVisible1=true;

                axios.get("/emp/list").then(result=>{
                    this.depts=result.data.data;
                })
            },

            add(){
                this.addForm.headimg=this.imageUrl;
                axios.post("/emp/add",this.addForm).then(result=>{
                    if(result.data.code===200){
                        this.$message.success("添加成功")
                        this.dialogVisible1=false;
                        this.initTable();
                        this.imageUrl="";
                        this.$refs.addFormRef.resetFields();
                        this.$nextTick(
                            ()=> {this.addForm = {}}
                        )
                    }
                })

            },
            editEmp(row){

                this.dialogVisible=true;
                this.editForm=row;

                axios.get("/dept/list").then(result=>{
                    this.depts=result.data.data;
                })
            },

            confirmUpdate(){
                this.editForm.headImg=this.imageUrl;
                axios.post("/emp/update",this.editForm).then(result=>{
                    if(result.data.code===200){
                        this.$message.success("修改员工信息成功")
                        this.dialogVisible=false;

                        this.initTable();
                    }
                })
            },

            del(row){
                // console.log(row);
                axios.get("/emp/delete?id="+row.id).then(result=>{
                    if(result.data.code ===200){
                        this.$message.success("删除成功");
                        this.initTable();
                    }else {
                        this.$message.success("删除失败");
                        this.initTable();

                    }
                })
            },
            onSubmit() {
                this.initTable();
            },
            initTable(){
                axios.post("/emp/list?currentPage="+this.currentPage+"&pageSize"+this.pageSize,this.empSearchForm).then(result=>{
                    if(result.data.code===200){
                        this.tableData=result.data.data.list;
                        // console.log(result);
                        this.total=result.data.data.total;
                    }
                })
            }
        }
    })
</script>



</body>
</html>

